<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-dummythread.html" />
<link rel="prev" href="module-thread.html" />
<link rel="parent" href="someos.html" />
<link rel="next" href="lock-objects.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>15.3 threading -- Higher-level threading interface</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="15.2 thread  "
  href="module-thread.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="15. optional Operating System"
  href="someos.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="15.3.1 lock Objects"
  href="lock-objects.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-thread.html">15.2 thread  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="someos.html">15. Optional Operating System</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="lock-objects.html">15.3.1 Lock Objects</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h1><a name="SECTION0017300000000000000000">
15.3 <tt class="module">threading</tt> --
         Higher-level threading interface</a>
</h1>

<p>
<a name="module-threading"></a>

<p>
This module constructs higher-level threading interfaces on top of the 
lower level <tt class="module"><a href="module-thread.html">thread</a></tt> module.

<p>
The <tt class="module"><a href="module-dummythreading.html">dummy_threading</a></tt> module is provided for
situations where <tt class="module">threading</tt> cannot be used because
<tt class="module"><a href="module-thread.html">thread</a></tt> is missing.

<p>
This module defines the following functions and objects:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3421' xml:id='l2h-3421' class="function">activeCount</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
Return the number of <tt class="class">Thread</tt> objects currently alive.  The
returned count is equal to the length of the list returned by
<tt class="function">enumerate()</tt>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3422' xml:id='l2h-3422' class="function">Condition</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
A factory function that returns a new condition variable object.
A condition variable allows one or more threads to wait until they
are notified by another thread.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3423' xml:id='l2h-3423' class="function">currentThread</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
Return the current <tt class="class">Thread</tt> object, corresponding to the
caller's thread of control.  If the caller's thread of control was not
created through the
<tt class="module">threading</tt> module, a dummy thread object with limited functionality
is returned.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3424' xml:id='l2h-3424' class="function">enumerate</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
Return a list of all <tt class="class">Thread</tt> objects currently alive.  The list
includes daemonic threads, dummy thread objects created by
<tt class="function">currentThread()</tt>, and the main thread.  It excludes
terminated threads and threads that have not yet been started.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3425' xml:id='l2h-3425' class="function">Event</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
A factory function that returns a new event object.  An event manages
a flag that can be set to true with the <tt class="method">set()</tt> method and
reset to false with the <tt class="method">clear()</tt> method.  The <tt class="method">wait()</tt>
method blocks until the flag is true.
</dl>

<p>
<dl><dt><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-3426' xml:id='l2h-3426' class="class">local</tt></b>
<dd>
A class that represents thread-local data.  Thread-local data are data
whose values are thread specific.  To manage thread-local data, just
create an instance of <tt class="class">local</tt> (or a subclass) and store
attributes on it:

<p>
<div class="verbatim"><pre>
mydata = threading.local()
mydata.x = 1
</pre></div>

<p>
The instance's values will be different for separate threads.

<p>
For more details and extensive examples, see the documentation string
of the <tt class="module">_threading_local</tt> module.

<p>

<span class="versionnote">New in version 2.4.</span>

</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3427' xml:id='l2h-3427' class="function">Lock</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
A factory function that returns a new primitive lock object.  Once
a thread has acquired it, subsequent attempts to acquire it block,
until it is released; any thread may release it.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3428' xml:id='l2h-3428' class="function">RLock</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
A factory function that returns a new reentrant lock object.
A reentrant lock must be released by the thread that acquired it.
Once a thread has acquired a reentrant lock, the same thread may
acquire it again without blocking; the thread must release it once
for each time it has acquired it.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3429' xml:id='l2h-3429' class="function">Semaphore</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>value</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
A factory function that returns a new semaphore object.  A
semaphore manages a counter representing the number of <tt class="method">release()</tt>
calls minus the number of <tt class="method">acquire()</tt> calls, plus an initial value.
The <tt class="method">acquire()</tt> method blocks if necessary until it can return
without making the counter negative.  If not given, <var>value</var> defaults to
1. 
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3430' xml:id='l2h-3430' class="function">BoundedSemaphore</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>value</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
A factory function that returns a new bounded semaphore object.  A bounded
semaphore checks to make sure its current value doesn't exceed its initial
value.  If it does, <tt class="exception">ValueError</tt> is raised. In most situations
semaphores are used to guard resources with limited capacity.  If the
semaphore is released too many times it's a sign of a bug.  If not given,
<var>value</var> defaults to 1. 
</dl>

<p>
<dl><dt><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-3431' xml:id='l2h-3431' class="class">Thread</tt></b>
<dd>
A class that represents a thread of control.  This class can be safely
subclassed in a limited fashion.
</dl>

<p>
<dl><dt><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-3432' xml:id='l2h-3432' class="class">Timer</tt></b>
<dd>
A thread that executes a function after a specified interval has passed.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3433' xml:id='l2h-3433' class="function">settrace</tt></b>(</nobr></td>
  <td><var>func</var>)</td></tr></table></dt>
<dd>
Set a trace function<a id='l2h-3436' xml:id='l2h-3436'></a> for all threads started
from the <tt class="module">threading</tt> module.  The <var>func</var> will be passed to 
<tt class="function">sys.settrace()</tt> for each thread, before its <tt class="method">run()</tt>
method is called.

<span class="versionnote">New in version 2.3.</span>

</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3434' xml:id='l2h-3434' class="function">setprofile</tt></b>(</nobr></td>
  <td><var>func</var>)</td></tr></table></dt>
<dd>
Set a profile function<a id='l2h-3437' xml:id='l2h-3437'></a> for all threads started
from the <tt class="module">threading</tt> module.  The <var>func</var> will be passed to 
<tt class="function">sys.setprofile()</tt> for each thread, before its <tt class="method">run()</tt>
method is called.

<span class="versionnote">New in version 2.3.</span>

</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3435' xml:id='l2h-3435' class="function">stack_size</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>size</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Return the thread stack size used when creating new threads.  The
optional <var>size</var> argument specifies the stack size to be used for
subsequently created threads, and must be 0 (use platform or
configured default) or a positive integer value of at least 32,768 (32kB).
If changing the thread stack size is unsupported, a <tt class="exception">ThreadError</tt>
is raised.  If the specified stack size is invalid, a <tt class="exception">ValueError</tt>
is raised and the stack size is unmodified.  32kB is currently the minimum
supported stack size value to guarantee sufficient stack space for the
interpreter itself.  Note that some platforms may have particular
restrictions on values for the stack size, such as requiring a minimum
stack size &gt; 32kB or requiring allocation in multiples of the system
memory page size - platform documentation should be referred to for
more information (4kB pages are common; using multiples of 4096 for
the stack size is the suggested approach in the absence of more
specific information).
Availability: Windows, systems with POSIX threads.

<span class="versionnote">New in version 2.5.</span>

</dl>

<p>
Detailed interfaces for the objects are documented below.  

<p>
The design of this module is loosely based on Java's threading model.
However, where Java makes locks and condition variables basic behavior
of every object, they are separate objects in Python.  Python's <tt class="class">Thread</tt>
class supports a subset of the behavior of Java's Thread class;
currently, there are no priorities, no thread groups, and threads
cannot be destroyed, stopped, suspended, resumed, or interrupted.  The
static methods of Java's Thread class, when implemented, are mapped to
module-level functions.

<p>
All of the methods described below are executed atomically.

<p>

<p><br /></p><hr class='online-navigation' />
<div class='online-navigation'>
<!--Table of Child-Links-->
<a name="CHILD_LINKS"><strong>Subsections</strong></a>

<ul class="ChildLinks">
<li><a href="lock-objects.html">15.3.1 Lock Objects</a>
<li><a href="rlock-objects.html">15.3.2 RLock Objects</a>
<li><a href="condition-objects.html">15.3.3 Condition Objects</a>
<li><a href="semaphore-objects.html">15.3.4 Semaphore Objects</a>
<ul>
<li><a href="semaphore-examples.html">15.3.4.1 <tt class="class">Semaphore</tt> Example</a>
</ul>
<li><a href="event-objects.html">15.3.5 Event Objects</a>
<li><a href="thread-objects.html">15.3.6 Thread Objects</a>
<li><a href="timer-objects.html">15.3.7 Timer Objects</a>
<li><a href="with-locks.html">15.3.8 Using locks, conditions, and semaphores in the <tt class="keyword">with</tt>
statement</a>
</ul>
<!--End of Table of Child-Links-->
</div>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="15.2 thread  "
  href="module-thread.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="15. optional Operating System"
  href="someos.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="15.3.1 lock Objects"
  href="lock-objects.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-thread.html">15.2 thread  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="someos.html">15. Optional Operating System</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="lock-objects.html">15.3.1 Lock Objects</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
